<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="用户ID" prop="customerId">
        <el-input v-model="queryParams.customerId" placeholder="请输入用户ID" clearable style="width: 200px" disabled />
      </el-form-item>
      <el-form-item label="聊天内容" prop="content">
        <el-input v-model="queryParams.content" placeholder="请输入聊天内容" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="消息类型" prop="messageType">
        <el-radio-group v-model="queryParams.messageType">
          <el-radio label="all">全部消息</el-radio>
          <el-radio label="user">用户消息</el-radio>
          <el-radio label="reply">回复消息</el-radio>
        </el-radio-group>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="messageList" style="width: 100%">
      <el-table-column label="对话ID" align="center" prop="id" width="75" />
      <el-table-column label="发送人" align="center" prop="messageFrom" width="350" />
      <el-table-column label="对话时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="对话内容" align="center" prop="message" :show-overflow-tooltip="true" />
      <el-table-column label="回复内容" align="center" prop="answer" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleViewDetail(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 对话详情弹窗 -->
    <el-dialog :title="'对话详情'" v-model="dialogVisible" width="600px" append-to-body>
      <div class="chat-container" style="height: 400px; overflow-y: auto;">
        <div class="message-item" style="margin-bottom: 20px;">
          <div class="user-message" style="text-align: left; margin-right: 50px;">
            <div style="background: #e5e5e5; padding: 10px; border-radius: 8px; display: inline-block; max-width: 80%;">
              {{ dialogMessage }}
            </div>
          </div>
          <div v-if="dialogAnswer" class="system-message" style="text-align: left; margin-left: 0px; margin-top: 20px;">
            <div style="background: #95ec69; padding: 10px 15px; border-radius: 8px; text-align: left; max-width: 80%;">
              {{ dialogAnswer }}
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="AppStoreMessage">
import { useRoute } from 'vue-router';
import { listMessage } from "@/api/app-store/message";

const { proxy } = getCurrentInstance();

const messageList = ref([]);
const loading = ref(true);
const total = ref(0);

// 对话详情相关变量
const dialogVisible = ref(false);
const dialogMessage = ref('');
const dialogAnswer = ref('');

const route = useRoute();

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  customerId: undefined,
  content: undefined,
  messageType: "all"
});

/** 查询消息列表 */
function getList() {
  loading.value = true;
  listMessage(queryParams.value).then(response => {
    messageList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查看详情按钮操作 */
function handleViewDetail(row) {
  dialogMessage.value = row.message;
  dialogAnswer.value = row.answer;
  dialogVisible.value = true;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.customerId = route.query.id;
  handleQuery();
}

// 初始化用户ID
queryParams.value.customerId = route.query.id;
getList();
</script>